
//globals
var camera, scene, renderer;
var clock, controls;
var stats;
var xLight, yLight, zLight;
var teapot;
var text;

//test some stuff here...
function fill_scene() {    
        
    //after doll.fetch using xhr to get the file a material is made
    //using the vs and fs
    doll.toon_shader.on_load = function () {
        console.log('I got my toon shader!');
        //shader = doll.toon_shader.copy();
        //mesh = new THREE.Mesh(geometry, shader);
        //shader.uniforms...
        //scene.add(mesh);
    }

    //scene.add(new THREE.Mesh(new THREE.CubeGeometry(1,1,1)), new THREE.MeshBasicMaterial({color: 0xffff00}));
    //scene.add(doll.text.print("null",0,0,1));

    zLight = new THREE.SpotLight(0xffffff, 0.5);
    zLight.position.set(0,0,100);
    scene.add(zLight);

    //use xhr to get the toon shader
    doll.toon_shader.fetch(false);
}


function add_stats() {
    stats = new Stats();
    stats.setMode(0); // 0: fps, 1: ms

    // Align top-left
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.left = '0px';
    stats.domElement.style.top = '0px';
    document.body.appendChild( stats.domElement );
}

var shader;
function init() {
    clock = new THREE.Clock();
    scene = new THREE.Scene();
    //first 75 or 45?
    //last 1000 or 80000?
    camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 80000 );
    //camera.position.set(-600, 500, 1300);
    camera.position.set(0, 0, 20);
    renderer = new THREE.WebGLRenderer( {antialias: true} );

    renderer.gammaInput = true;
    renderer.gammaOutput = true;
    renderer.setClearColor(0xaaaaaa,1.0);
    renderer.setSize( window.innerWidth, window.innerHeight );

    //init engine
    doll.ui.init();
    doll.text.init();
    doll.fetch.init();
    doll.audio.init();
    doll.camera = camera;//current camera global
    doll.scene = scene; //current scene global
    doll.window_width = window.innerWidth;
    doll.window_height = window.innerHeight;

    document.body.appendChild( renderer.domElement );
}

function setup_audio() {
    //var bg =  'assets/audio/kpp_revolution.ogg';
    //var bg =  'assets/audio/oniichan_09.ogg';
    //var bg =  'assets/audio/galaxy/02.wav';
    var audio = [
//        'ponponpon.wav',
//        'oniichan_09.ogg'
    ];
    doll.audio.complete = function() {
        console.log('hey I got all my audio');
        //doll.audio.play(1);
    }
    doll.audio.fetch(audio, false);
}

function animate() {
    requestAnimationFrame(animate, renderer.domElement);
    render();
}

function render() {
    setInterval( function () {
        console.profile('render');
        stats.begin();
    
        var delta = clock.getDelta();

        doll.ui.animate(delta);
        renderer.render(scene, camera);
        stats.end();
        console.profileEnd('render');
    }, 1000/60);
}

init();
setup_audio();
fill_scene();
add_stats();
animate();
